package com.kaifamiao.comparing;

import java.util.Arrays;

public class CompareMonkey {

    public static void main(String[] args) {

        Monkey first = new Monkey( "孙悟空", 18, 20, 50 );
        Monkey second = new Monkey( "六耳猕猴", 16, 20, 50 );

        if( first.getAge() > second.getAge() ) {
            System.out.println( first.getName() + "比" + second.getName() + "年龄大" );
        }

        System.out.println( "- ".repeat( 10 ) );

        Monkey[] monkeys = new Monkey[5];
        System.out.println( Arrays.toString( monkeys ) );

        monkeys[0] = new Monkey( "吉吉国王", 30, 40, 50 );
        monkeys[1] = new Monkey( "毛毛", 22, 19, 42 );
        monkeys[2] = first;
        monkeys[3] = second;
        monkeys[4] = new Monkey( "通臂猿猴" , 25, 35, 55 );

        System.out.println( Arrays.toString( monkeys ) );

        /*
        // 用冒泡实现排序 (根据年龄升序排列)
        for (int i = 0; i < monkeys.length - 1 ; i++) {
            for (int j = 0; j < monkeys.length - 1 -i ; j++) {
                Monkey x = monkeys[j];
                Monkey y = monkeys[j+1];
                // if( x.getAge() > y.getAge() ) {
                if( x.compareTo( y ) > 0 ) {
                    monkeys[j] = y;
                    monkeys[j+1] = x;
                }
            }
        }
        */

        // 调用 Arrays 的 sort 方法 对 monkeys 进行 整体排序
        // Arrays.sort( monkeys );

        // 调用 Arrays 的 sort 方法 对 monkeys 进行 局部排序
        Arrays.sort( monkeys, 2, 5 );

        System.out.println( Arrays.toString( monkeys ) );

    }

}
